package wetalk.software.bupt.com.wetalk.BroadcastReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import wetalk.software.bupt.com.wetalk.DB.WeTalkDB;
import wetalk.software.bupt.com.wetalk.application.MyApplication;
import wetalk.software.bupt.com.wetalk.model.po.Group;

/**
 * Created by whc07 on 2018/3/17.
 */

public class PullGroupReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String content=intent.getStringExtra("content");
        String type=intent.getStringExtra("type");
        SharedPreferences.Editor editor= MyApplication.getContext().getSharedPreferences("data",Context.MODE_PRIVATE).edit();
        WeTalkDB.create(MyApplication.getContext()).deleteAllGroup();
        try {
            JSONArray jsonArray=new JSONArray(content);
            for (int i = 0; i <jsonArray.length() ; i++) {
                JSONObject obj=jsonArray.getJSONObject(i);
                Group group = new Group();
                group.setGroup_id(obj.getString("gid")+"_g");
                group.setGroup_name(obj.getString("gname"));
                group.setUser_id(obj.getString("member"));
                WeTalkDB.create(MyApplication.getContext()).addGroup(group);
            }
            editor.putString("receive","ok");
            editor.apply();

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}
